package jh.com.jhappbf.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.util.AbStrUtil;
import com.ab.view.calendar.CalendarCell;
import com.ab.view.calendar.CalendarView;
import com.ab.view.titlebar.AbTitleBar;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

import androidx.core.content.ContextCompat;
import jh.com.jhappbf.R;

public class CalendarActivity extends AbActivity {
    private CalendarView mCalendarView = null;
    private List<String> monthList = null;
    private int currentMonthIndex = 0;
    private TextView monthText = null;
    private String currentMonth = null;
    private AbTitleBar mAbTitleBar = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.calendar);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText(R.string.calendar_name);
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);

        initTitleRightLayout();

        LinearLayout mLinearLayout = this.findViewById(R.id.layout01);
        mCalendarView = new CalendarView(this);
        mLinearLayout.addView(mCalendarView);
        initTitleRightLayout();

        mCalendarView.setHeaderHeight(45);
        mCalendarView.setHeaderTextSize(20);

        mCalendarView.setBackgroundResource(R.drawable.calendar_bg);
        mCalendarView.setHeaderBackgroundResource(R.drawable.week_bg);
        mCalendarView.setOnItemClickListener(new CalendarView.AbOnItemClickListener() {

            @Override
            public void onClick(int position) {
                String date = mCalendarView.getStrDateAtPosition(position);
                Intent i = new Intent();
                i.putExtra("date", date);
                CalendarActivity.this.setResult(1, i);
                CalendarActivity.this.finish();
                //AbToastUtil.showToast(CalendarActivity.this,"点击了"+position+"值："+date);
            }
        });

        Calendar calendar = Calendar.getInstance();
        monthList = new ArrayList<>();
        //
        int curYear = calendar.get(Calendar.YEAR);
        int curMonth = calendar.get(Calendar.MONTH) + 1;

        int preYear = curYear - 1;
        for (int i = 1; i < 12; i++) {
            monthList.add(preYear + "-" + AbStrUtil.strFormat2(String.valueOf(i)));
        }

        for (int i = 1; i <= curMonth; i++) {
            monthList.add(curYear + "-" + AbStrUtil.strFormat2(String.valueOf(i)));
        }

        currentMonthIndex = monthList.size() - 1;
        currentMonth = monthList.get(currentMonthIndex);

        monthText = findViewById(R.id.monthText);
        monthText.setText(currentMonth);

        Button leftBtn = findViewById(R.id.leftBtn);
        Button rightBtn = findViewById(R.id.rightBtn);

        leftBtn.setOnClickListener(v -> {
            currentMonthIndex--;
            if (currentMonthIndex >= 0) {
                currentMonth = monthList.get(currentMonthIndex);
                monthText.setText(currentMonth);
                String[] yearMonth = currentMonth.split("-");
                Calendar calSelect = Calendar.getInstance();
                calSelect.set(Calendar.YEAR, Integer.parseInt(yearMonth[0]));
                calSelect.set(Calendar.MONTH, Integer.parseInt(yearMonth[1]) - 1);
                calSelect.set(Calendar.DAY_OF_MONTH, 1);
                mCalendarView.rebuildCalendar(calSelect);
                ArrayList<CalendarCell> mCalendarCell = mCalendarView.getCalendarCells();
                for (int i = 0; i < 5; i++) {
                    CalendarCell cc = mCalendarCell.get(new Random().nextInt(mCalendarCell.size()));
                    //有数据
                    cc.setHasRecord(true);
                }
                //showProgressDialog();
                //netGet.downloadBeforeClean(item1);
            } else {
                currentMonthIndex++;
            }
        });

        rightBtn.setOnClickListener(v -> {
            currentMonthIndex++;
            if (currentMonthIndex < monthList.size()) {
                currentMonth = monthList.get(currentMonthIndex);
                monthText.setText(currentMonth);
                String[] yearMonth = currentMonth.split("-");
                Calendar calSelect = Calendar.getInstance();
                calSelect.set(Calendar.YEAR, Integer.parseInt(yearMonth[0]));
                calSelect.set(Calendar.MONTH, Integer.parseInt(yearMonth[1]) - 1);
                calSelect.set(Calendar.DAY_OF_MONTH, 1);
                mCalendarView.rebuildCalendar(calSelect);
                ArrayList<CalendarCell> mCalendarCell = mCalendarView.getCalendarCells();
                for (int i = 0; i < 5; i++) {
                    CalendarCell cc = mCalendarCell.get(new Random().nextInt(mCalendarCell.size()));
                    //有数据
                    cc.setHasRecord(true);
                }
                //mActivity.showProgressDialog();
                //netGet.downloadBeforeClean(item1);
            } else {
                currentMonthIndex--;
            }
        });

        ArrayList<CalendarCell> mCalendarCell = mCalendarView.getCalendarCells();
        for (int i = 0; i < 5; i++) {
            CalendarCell cc = mCalendarCell.get(new Random().nextInt(mCalendarCell.size()));
            //有数据
            cc.setHasRecord(true);
        }

    }

    private void initTitleRightLayout() {
        mAbTitleBar.clearRightView();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();

    }
}
